package com.dcy.znoa.controller.publiz;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.department.AddDepartmentModel;
import com.dcy.znoa.controller.publiz.model.department.SelectDepartmentModel;
import com.dcy.znoa.controller.publiz.model.department.UpdateDepartmentModel;
import com.dcy.znoa.controller.publiz.model.department.LogicDelDepartmentModel;
import com.dcy.znoa.dao.publiz.DepartmentDao;
import com.dcy.znoa.dao.user.OperDao;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.publiz.Department;
import com.dcy.znoa.service.publiz.DepartmentService;
import com.dcy.znoa.service.software.OperService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import static com.dcy.znoa.common.util.pojo.JsonCommon.DELETE_ERROR_CODE;
import static com.dcy.znoa.common.util.pojo.JsonCommon.SUCCESS_CODE;
import static com.dcy.znoa.domain.publiz.Department.TO_AUDIT;

/**
 * @author pengyu
 */
//企业组织 controller
@Controller
@RequestMapping("dep")
public class DepartmentController extends BaseController {
    @Autowired
    DepartmentService departmentService;
    @Autowired
    OperService operService;
    @Autowired
    DepartmentDao departmentDao;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(SelectDepartmentModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
        return departmentService.selectList(model);
    }


    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectByCompId")
    public JsonForList selectByCompId(Integer compId) throws BusinessException, InvocationTargetException, IllegalAccessException, InstantiationException {
        return departmentService.selectByCompId(compId);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddDepartmentModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        if(model.getDepName().contains("审批")){//TO_AUDIT  审批 字样的都不行
            return  new JsonCommon("404","部门名称中不能出现审批关键字");
        }
        model.setCreateOperId(getCurrentUser().getOperId());
        Department cp = new Department();
        BeanUtils.copyProperties(cp, model);
        return departmentService.insert(cp);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateDepartmentModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Department cp = new Department();
        BeanUtils.copyProperties(cp, model);
        return departmentService.updateById(cp);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/logicDeleteById")
    public JsonCommon logicDeleteById(@Valid LogicDelDepartmentModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        return deleteById(model, bindingResult);//pengyu modify

//                if (bindingResult.hasErrors()){
//            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
//        }
//        //查询当前组织下有没有用户，有就不准执行
//        Oper query = new Oper();
//        query.setDepId(model.getDepId());
//        List<Oper> list =  operService.selectUserList(query);
//        if(!list.isEmpty()){
//            return  new JsonCommon("804","拥有员工的部门不允许禁用");
//        }
//        //级别为1的不允许删除，会影响组织加载
//        Department department = departmentDao.selectById(model.getDepId());
//        if(department.getDepLevel() != null && department.getDepLevel() == 1){
//            return  new JsonCommon("804","公司根节点部门不允许被删除，如有需要请联系平台管理员");
//        }
//        if(department.getDepName().equals(TO_AUDIT)){
//            return  new JsonCommon("804",TO_AUDIT+"部门不允许被删除，如有需要请联系平台管理员");
//        }
//        //TODO 理论上有附属部门的部门也不能被删除
//        model.setUpdateOperId(getCurrentUser().getOperId());
//        Department cp = new Department();
//        BeanUtils.copyProperties(cp, model);
//        return departmentService.updateById(cp);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/deleteById")
    public JsonCommon deleteById(@Valid LogicDelDepartmentModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        //查询当前组织下有没有用户，有就不准执行
        Oper query = new Oper();
        query.setDepId(model.getDepId());
        List<Oper> list =  operService.selectUserList(query);
        if(!list.isEmpty()){
            return  new JsonCommon("804","拥有员工的部门不允许禁用");
        }
        //级别为1的不允许删除，会影响组织加载
        Department department = departmentDao.selectById(model.getDepId());
        if(department.getDepLevel() != null && department.getDepLevel() == 1){
            return  new JsonCommon("804","公司根节点部门不允许被删除，如有需要请联系平台管理员");
        }
        if(department.getDepName().equals(TO_AUDIT)){
            return  new JsonCommon("804",TO_AUDIT+"部门不允许被删除，如有需要请联系平台管理员");
        }
        Integer result = departmentService.deleteById(model.getDepId());
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }
        return new JsonCommon(DELETE_ERROR_CODE);
    }

}
